package com.wlts.wzy.TestDemo;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.linkedmall.model.v20180116.*;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

public class QueryItemDemo {
    private static String ACCESS_KEY_ID = "LTAI5t8yW2aRDCbZZv6vE323";
    private static String ACCESS_KEY_SECRET = "qm3hBdoelUQmQGMD3C4lZKzxa8MKrJ";
    private static String LZ_BIZ_ID = "LMALL202104260001";//绿账科
//    private static String ACCESS_KEY_ID = "LTAI4GBmnzKWhYzC8zHmzJt8";
//    private static String ACCESS_KEY_SECRET = "DxkVtMxqZKC8zFUgr2lEuB1v33PU2R";
//    private static String LZ_BIZ_ID = "LMALL202101210003";//绿账科
    private static String HZ_BIZ_ID = "LMALL202009080002";//支付宝活动

    private static DefaultProfile profile;
    private static IAcsClient client;

    static {
        profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        client = new DefaultAcsClient(profile);
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    public static QueryBizItemListResponse queryItemList() {

        QueryBizItemListRequest request = new QueryBizItemListRequest();
        request.setBizId(LZ_BIZ_ID);
        request.setPageNumber(1);
        request.setPageSize(10);

        try {
            QueryBizItemListResponse resp = client.getAcsResponse(request);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询商品详情信息（商品详情信息）
     *
     * @param itemId
     * @return
     */
    public static QueryItemDetailResponse queryItemDetail(Long itemId) {

        QueryItemDetailRequest request = new QueryItemDetailRequest();
        request.setBizId(LZ_BIZ_ID);
        request.setItemId(itemId);
        //  request.setPageSize(10);

        try {
            QueryItemDetailResponse resp = client.getAcsResponse(request);

            System.out.println(JSON.toJSONString(resp));

            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询地址区号
     *
     * @return
     */
    public static QueryAddressResponse QueryAddress() {

        //{"divisionCode":310000,"divisionLevel":2,"divisionName":"上海","parentId":1},{"divisionCode":530000,"divisionLevel":2,"divisionName":"云南省","parentId":1},{"divisionCode":150000,"divisionLevel":2,"divisionName":"内蒙古自治区","parentId":1},{"divisionCode":110000,"divisionLevel":2,"divisionName":"北京","parentId":1},{"divisionCode":220000,"divisionLevel":2,"divisionName":"吉林省","parentId":1},{"divisionCode":510000,"divisionLevel":2,"divisionName":"四川省","parentId":1},{"divisionCode":120000,"divisionLevel":2,"divisionName":"天津","parentId":1},{"divisionCode":640000,"divisionLevel":2,"divisionName":"宁夏回族自治区","parentId":1},{"divisionCode":340000,"divisionLevel":2,"divisionName":"安徽省","parentId":1},{"divisionCode":370000,"divisionLevel":2,"divisionName":"山东省","parentId":1},{"divisionCode":140000,"divisionLevel":2,"divisionName":"山西省","parentId":1},{"divisionCode":440000,"divisionLevel":2,"divisionName":"广东省","parentId":1},{"divisionCode":450000,"divisionLevel":2,"divisionName":"广西壮族自治区","parentId":1},{"divisionCode":650000,"divisionLevel":2,"divisionName":"新疆维吾尔自治区","parentId":1},{"divisionCode":320000,"divisionLevel":2,"divisionName":"江苏省","parentId":1},{"divisionCode":360000,"divisionLevel":2,"divisionName":"江西省","parentId":1},{"divisionCode":130000,"divisionLevel":2,"divisionName":"河北省","parentId":1},{"divisionCode":410000,"divisionLevel":2,"divisionName":"河南省","parentId":1},{"divisionCode":330000,"divisionLevel":2,"divisionName":"浙江省","parentId":1},{"divisionCode":460000,"divisionLevel":2,"divisionName":"海南省","parentId":1},{"divisionCode":420000,"divisionLevel":2,"divisionName":"湖北省","parentId":1},{"divisionCode":430000,"divisionLevel":2,"divisionName":"湖南省","parentId":1},{"divisionCode":620000,"divisionLevel":2,"divisionName":"甘肃省","parentId":1},{"divisionCode":350000,"divisionLevel":2,"divisionName":"福建省","parentId":1},{"divisionCode":540000,"divisionLevel":2,"divisionName":"西藏自治区","parentId":1},{"divisionCode":520000,"divisionLevel":2,"divisionName":"贵州省","parentId":1},{"divisionCode":210000,"divisionLevel":2,"divisionName":"辽宁省","parentId":1},{"divisionCode":500000,"divisionLevel":2,"divisionName":"重庆","parentId":1},{"divisionCode":610000,"divisionLevel":2,"divisionName":"陕西省","parentId":1},{"divisionCode":630000,"divisionLevel":2,"divisionName":"青海省","parentId":1},{"divisionCode":230000,"divisionLevel":2,"divisionName":"黑龙江省","parentId":1}


        //上海 310000  --2
        //上海市 310100  --3
        //嘉定区 310114  --4
        //华亭镇 310114111  --5

        QueryAddressRequest request = new QueryAddressRequest();
        request.setBizId(LZ_BIZ_ID);
        request.setDivisionCode("1");
        //request.setItemId(itemId);
        //  request.setPageSize(10);

        try {
            QueryAddressResponse resp = client.getAcsResponse(request);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 创建订单
     *
     * @return
     */
    public static CreateOrderV2Response CreateOrderV2() {

        CreateOrderV2Request request = new CreateOrderV2Request();
        CreateOrderV2Request.ItemList itemList = new CreateOrderV2Request.ItemList();
        DeliveryAddress deliveryAddress = new DeliveryAddress();

        request.setBizId(LZ_BIZ_ID);
        request.setAccountType("ANONY");
        request.setThirdPartyUserId("15895437");
        request.setBizUid("15895437");
        request.setOutTradeId("4562859658744457");

        List<CreateOrderV2Request.ItemList> itemLists = new ArrayList<>();

        itemList.setLmItemId("10000102-635471784928");
        itemList.setSkuId(4552381692878L);
        itemList.setQuantity(1);
        itemLists.add(itemList);
        request.setItemLists(itemLists);

        deliveryAddress.setFullName("测试666");
        deliveryAddress.setMobile("15813477478");
        deliveryAddress.setAddressDetail("测试订单别发");
        deliveryAddress.setDivisionCode("330106");
        request.setDeliveryAddress(JSON.toJSONString(deliveryAddress));

        try {
            CreateOrderV2Response resp = client.getAcsResponse(request);

            System.out.println(JSON.toJSONString(request));
            System.out.println(JSON.toJSONString(resp));

            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 创建订单
     *
     * @return
     */
    public static CreateOrderV2Response CreateOrderV2List(List<CreateOrder> orderList) {

        orderList.forEach(order->{

            CreateOrderV2Request request = new CreateOrderV2Request();
            CreateOrderV2Request.ItemList itemList = new CreateOrderV2Request.ItemList();
            DeliveryAddress deliveryAddress = new DeliveryAddress();
            request.setBizId(LZ_BIZ_ID);
            request.setAccountType("ANONY");
            request.setThirdPartyUserId("kuaisou001");
            request.setBizUid(request.getThirdPartyUserId());

            List<CreateOrderV2Request.ItemList> itemLists = new ArrayList<>();


            //江苏省 320000
            // 无锡市 320200
            // 江阴市 320281

            request.setOutTradeId(order.getOutTradeId());
            itemList.setLmItemId(order.getLmItemId());
            itemList.setSkuId(order.getSkuId());

            deliveryAddress.setFullName(order.getFullName());
            deliveryAddress.setMobile(order.getMobile());
            deliveryAddress.setAddressDetail(order.getAddressDetail());
            deliveryAddress.setDivisionCode(order.getDivisionCode());
            deliveryAddress.setTownDivisionCode(order.getTownDivisionCode());

            itemLists.add(itemList);
            request.setItemLists(itemLists);

            request.setDeliveryAddress(JSON.toJSONString(deliveryAddress));

            try {
                CreateOrderV2Response resp = client.getAcsResponse(request);

                System.out.println(JSON.toJSONString(resp));

                enableOrder(request.getOutTradeId(), resp.getModel().getLmOrderList().get(0).getLmOrderId());

            } catch (Exception e) {
                e.printStackTrace();

            }


        });
        return null;

    }


    /**
     * 支付订单
     *
     * @return
     */
    public static EnableOrderResponse enableOrder(String outOrderId, String lmOrderId) {

        //{"code":"SUCCESS","logsId":"","message":"操作成功，但是创建支付单失败，请到订单列表点击继续支付。","model":{"lmOrderList":[{"lmOrderId":"243467676"}],"orderIds":["1541205903044906332"],"payTradeIds":[],"redirectUrl":"https://pages.tmall.com/wow/wt/act/lm-pages?wh_page=order-list"},"requestId":"0B2A116C-BD1A-4467-B68C-4783D163F0A8","subCode":"","subMessage":"","success":true}
        EnableOrderRequest request = new EnableOrderRequest();
        request.setBizId(LZ_BIZ_ID);
        request.setOutTradeId(outOrderId);
        request.setAccountType("ANONY");
        request.setThirdPartyUserId("2117889290");
        request.setBizUid(request.getThirdPartyUserId());
        request.setLmOrderId(lmOrderId);

//        request.setItemId(itemId);
//        //  request.setPageSize(10);

        try {
            EnableOrderResponse resp = client.getAcsResponse(request);

            System.out.println(JSON.toJSONString(resp));

            //{"code":"WITHHOLD_RESULT_STATUS_UNKOWN","logsId":"","message":"RESULT_STATUS_UNKOWN","model":{"lmOrderList":[],"orderIds":[],"payTradeIds":[],"redirectUrl":"RESULT_STATUS_UNKOWN"},"requestId":"B1AC36AC-903D-47EB-839A-A7AA442979A4","subCode":"","subMessage":"","success":true}
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    public static void main(String[] args) {
//        QueryItemDemo queryItemDemo = new QueryItemDemo();
//        Gson gson = new Gson();
//
//        QueryAddressResponse queryAddressResponse = queryItemDemo.QueryAddress();
//
//        System.out.println(gson.toJson(profile));
//
//        System.out.println(gson.toJson(queryAddressResponse));
//
////        QueryBizItemListResponse resp = queryItemDemo.queryItemList();
////        System.out.println(gson.toJson(resp));



//        List<QueryBizItemListResponse.Item> itemList = resp.getItemList();
//
//        itemList.forEach(item -> System.out.println(gson.toJson(item)));




        CreateOrderV2();
        //   QueryAddress();

//                  enableOrder("2105100006394373","264213253");

        //      queryItemDetail(607168817466L);

        /**
         * 骆哥下单信息 {"code":"SUCCESS","logsId":"","message":"操作成功，但是创建支付单失败，请到订单列表点击继续支付。","model":{"lmOrderList":[{"lmOrderId":"244605847"}],"orderIds":["1542145609021906332"],"payTradeIds":[],"redirectUrl":"https://pages.tmall.com/wow/wt/act/lm-pages?wh_page=order-list"},"requestId":"4A99868C-A22B-4DCB-86C7-5743A0843580","subCode":"","subMessage":"","success":true}
         */
//        快手单号：2102700028319090
//        linkedMall单号：244605847
        //       商品ItemId：630215920636
        //       商品skuId：630215920636

        /**
         * 支付信息 {"code":"SUCCESS","logsId":"","message":"","model":{"lmOrderList":[],"orderIds":[],"payTradeIds":[],"redirectUrl":"RESULT_STATUS_SUCCESS"},"requestId":"1C9F23E0-A95B-4F6D-9903-FD63CC73B753","subCode":"","subMessage":"","success":true}
         */


        /**
         * list 创建订单
         */

 //       List<CreateOrder> createOrderList=new ArrayList<>();



//        CreateOrder order1=new CreateOrder();
//
//        order1.setOutTradeId("4758708418331049603");
//        order1.setSkuId(4555506744142L);
//        order1.setLmItemId("10000102-636717343530");
//        order1.setFullName("顾晓婷");
//        order1.setMobile("13812139578");
//        order1.setDivisionCode("320281");
//        order1.setTownDivisionCode("320281005");
//        order1.setAddressDetail("江苏省 无锡市 江阴市  金科东方王府155单元402室");
//
//
//
//        CreateOrder order2=new CreateOrder();
//
//        order2.setOutTradeId("4758708517111370832");
//        order2.setSkuId(4555506744142L);
//        order2.setLmItemId("10000102-636717343530");
//        order2.setFullName("柳力瑞");
//        order2.setMobile("13919888489");
//        //城关区/620102	620102006
//
//        order2.setDivisionCode("620102");
//        order2.setTownDivisionCode("620102006");
//        order2.setAddressDetail("甘肃省 兰州市 城关区  庆阳路322号");
//
//
//        CreateOrder order3=new CreateOrder();
//
//        order3.setOutTradeId("4758707774085725760");
//        order3.setSkuId(4555506744142L);
//        order3.setLmItemId("10000102-636717343530");
//        order3.setFullName("垚尧");
//        order3.setMobile("19957828673");
//        //余杭区/330110	330106013
//
//        order3.setDivisionCode("330110");
//        order3.setTownDivisionCode("330106013");
//        order3.setAddressDetail("浙江省 杭州市 余杭区  文一西路西溪蓝海9号楼");
//
//        CreateOrder order4=new CreateOrder();
//
//        order4.setOutTradeId("4758708388274101710");
//        order4.setSkuId(4555506744142L);
//        order4.setLmItemId("10000102-636717343530");
//        order4.setFullName("戴蓉蓉");
//        order4.setMobile("13761209935");
//        //浦东新区/310115	310115110
//        order4.setDivisionCode("310115");
//        order4.setTownDivisionCode("310115110");
//        order4.setAddressDetail("上海市 市辖区 浦东新区  合庆镇春雷村后李家宅105号");
//
//
//        CreateOrder order5=new CreateOrder();
//
//        order5.setOutTradeId("4758707825630881150");
//        order5.setSkuId(4555506744142L);
//        order5.setLmItemId("10000102-636717343530");
//        order5.setFullName("彭璐");
//        order5.setMobile("18711347651");
//        //湘潭县/430321	430321100
//        order5.setDivisionCode("430321");
//        order5.setTownDivisionCode("430321100");
//        order5.setAddressDetail("湖南省 湘潭市 湘潭县  梅林桥镇黄竹村四新组");
//
//
//        CreateOrder order6=new CreateOrder();
//
//        order6.setOutTradeId("4758708139156891405");
//        order6.setSkuId(4555506744142L);
//        order6.setLmItemId("10000102-636717343530");
//        order6.setFullName("郭振云");
//        order6.setMobile("18089151002");
//        //余杭区/330110	330110012
//        order6.setDivisionCode("330110");
//        order6.setTownDivisionCode("330110012");
//        order6.setAddressDetail("浙江省 杭州市 余杭区  仓前街道万通时尚公馆2号楼1608");


//        CreateOrder order7=new CreateOrder();
//
//        order7.setOutTradeId("4758711605192316072");
//        order7.setSkuId(4549611461422L);
//        order7.setLmItemId("10000102-634908296908");
//        order7.setFullName("唐娟");
//        order7.setMobile("15982263272");
//        //余杭区/330110	330110012
//        order7.setDivisionCode("330110");
//        order7.setTownDivisionCode("330110012");
//        order7.setAddressDetail("四川省 成都市 金堂县  金鶴苑小区五栋二单元10号");

//
//        createOrderList.add(order1);
//        createOrderList.add(order2);
//        createOrderList.add(order3);
//        createOrderList.add(order4);
//        createOrderList.add(order5);
//        createOrderList.add(order6);
 //       createOrderList.add(order7);

 //       CreateOrderV2List(createOrderList);

    }

    @Data
    static class DeliveryAddress {

        private String divisionCode;

        private String townDivisionCode;

        private String fullName;

        private String mobile;

        private String addressDetail;

    }

@Data
static
class CreateOrder {

        private String outTradeId;

        private String lmItemId;

        private Long skuId;

        private String fullName;

        private String mobile;

        private String addressDetail;

        private String divisionCode;

        private String townDivisionCode;


    }

}
